import { Http } from '../Utils/Api'
import { removeObjectObj } from '../Utils'

// 获取页面默认配置与数据
export const getInitMenu = async ({ url, config, defaultPagig }) => {
    const data = await Http({ url, params: defaultPagig })
    const page = await Http({ url: 'getJSONData', params: { uuid: `Menu/${config}` } })
    const sort = page.sortConfig
        .filter(v => v.init)
        .reduce((acc, { value, init }, index) => {
            acc[value] = `${init}@${index}`
            return acc
        }, {})
    return { data, page, sort }
}

export const getMenuConfig = async ({ config, setPagina, setConfig }) => {
    const page = await Http({ url: 'getJSONData', params: { uuid: `Menu/${config}` } })
    const sort = page.sortConfig
        .filter(v => v.init)
        .reduce((acc, { value, init }, index) => {
            acc[value] = `${init}@${index}`
            return acc
        }, {})
    setPagina(e => ({ ...e, ...sort }))
    setConfig(page)
}

export const getTopOptions = (data) => {
    const {
        refreshStatus,
        queryStatus,
        sortStatus,
        addedStatus,
        addedType,
        addedUrl,
        addedExcel,
        addedParams,
        exportStatus,
        exportUrl,
        exportParams,
        batchDeletionStatus,
        batchDeletionUrl,
        batchDeletionParams,
        batchActivationStatus,
        batchActivationUrl,
        batchActivationParams,
        batchReviewStatus,
        batchReviewUrl,
        batchReviewParams,
    } = data

    const config = {
        refresh: refreshStatus || undefined,
        search: queryStatus || undefined,
        sortData: sortStatus || undefined,
        added: addedStatus ? { type: addedType, url: addedUrl, get: addedExcel, params: addedParams } : undefined,
        export: exportStatus ? { url: exportUrl, params: exportParams } : undefined,
        batchDeletion: batchDeletionStatus ? { url: batchDeletionUrl, params: batchDeletionParams } : undefined,
        batchIsEnabled: batchActivationStatus ? { url: batchActivationUrl, params: batchActivationParams } : undefined,
        batchReview: batchReviewStatus ? { url: batchReviewUrl, params: batchReviewParams } : undefined,
    }

    return Object.keys(config).reduce((acc, key) => {
        if (config[key] !== undefined) { acc[key] = config[key] }
        return acc
    }, {})
}

export const getInitData = async ({ url, paging, setTotal, setDataSource }) => {
    const data = await Http({ url, params: paging })
    const { list, total } = data.data
    setDataSource(list)
    setTotal(total)
}

export const menuRefresh = ({ Pagina, setTotal, setDataSource, setPagina, refreshData }) => {
    setTotal(0)
    setDataSource()
    setPagina(Pagina)
    refreshData(Pagina)
}

export const emitFunction = ({
    val,
    field = 'organizationUuid',
    Pagina,
    sortData,
    setSortData,
    searchData,
    setSearchData,
    setPagina,
    refreshData,
}) => {
    const { type, data } = val
    if (type === 'onTree') {
        const params = { ...Pagina, [field]: data }
        setPagina(params)
        refreshData(params)
    }
    if (type === 'onSort') {
        setSortData(data)
        const params = { ...removeObjectObj(Pagina, sortData), ...data }
        setPagina(params)
        refreshData(params)
    }
    if (type === 'onSearch') {
        setSearchData(data)
        const params = { ...removeObjectObj(Pagina, searchData), ...data }
        setPagina(params)
        refreshData(params)
    }
    if (type === 'onPagina') {
        const params = { ...Pagina, ...data }
        setPagina(params)
        refreshData(params)
    }
}